package com.ruoyi.labsafety.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labsafety.domain.LabSafetyCheckForm;
import com.ruoyi.labsafety.service.ILabSafetyCheckFormService;

/**
 * 安全检查单Controller
 * 
 * @author winner pan
 * @date 2022-03-27
 */
@RestController
@RequestMapping("/labsafety/labsafetyForm")
public class LabSafetyCheckFormController extends BaseController
{
    @Autowired
    private ILabSafetyCheckFormService labSafetyCheckFormService;

    /**
     * 查询安全检查单列表
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:query')")
    @GetMapping("/list")
    public TableDataInfo list(LabSafetyCheckForm labSafetyCheckForm)
    {
        startPage();
        List<LabSafetyCheckForm> list = labSafetyCheckFormService.selectLabSafetyCheckFormList(labSafetyCheckForm);
        return getDataTable(list);
    }

    /**
     * 导出安全检查单列表
     */
   // @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:export')")
   /* @Log(title = "安全检查单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabSafetyCheckForm labSafetyCheckForm)
    {
        List<LabSafetyCheckForm> list = labSafetyCheckFormService.selectLabSafetyCheckFormList(labSafetyCheckForm);
        ExcelUtil<LabSafetyCheckForm> util = new ExcelUtil<LabSafetyCheckForm>(LabSafetyCheckForm.class);
        return util.exportExcel(list, "labsafetyForm");
    }*/

    /**
     * 获取安全检查单详细信息
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:query')")
    @GetMapping(value = "/{instanceId}")
    public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId)
    {
        return AjaxResult.success(labSafetyCheckFormService.selectLabSafetyCheckFormById(instanceId));
    }

    /**
     * 新增安全检查单
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:edit')")
    @Log(title = "安全检查单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabSafetyCheckForm labSafetyCheckForm)
    {
        String[] safetyTemplateOptArray = labSafetyCheckForm.getSafetyTemplateOptArray();
        String safetyTemplateOpt = StringUtils.join(safetyTemplateOptArray, ",");
        labSafetyCheckForm.setSafetyTemplateOpt(safetyTemplateOpt);
        labSafetyCheckFormService.insertLabSafetyCheckForm(labSafetyCheckForm);
        return AjaxResult.success(labSafetyCheckForm);
    }

    /**
     * 新增安全检查单
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:edit')")
    @Log(title = "安全检查单", businessType = BusinessType.INSERT)
    @PostMapping("submit")
    public AjaxResult submit(@RequestBody LabSafetyCheckForm labSafetyCheckForm)
    {
        String[] safetyTemplateOptArray = labSafetyCheckForm.getSafetyTemplateOptArray();
        String safetyTemplateOpt = StringUtils.join(safetyTemplateOptArray, ",");
        labSafetyCheckForm.setSafetyTemplateOpt(safetyTemplateOpt);

        labSafetyCheckFormService.submitLabSafetyCheckForm(labSafetyCheckForm);
        return AjaxResult.success(labSafetyCheckForm);
    }

    /**
     * 修改安全检查单
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:edit')")
    @Log(title = "安全检查单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabSafetyCheckForm labSafetyCheckForm)
    {
        String[] safetyTemplateOptArray = labSafetyCheckForm.getSafetyTemplateOptArray();
        String safetyTemplateOpt = StringUtils.join(safetyTemplateOptArray, ",");
        labSafetyCheckForm.setSafetyTemplateOpt(safetyTemplateOpt);
        labSafetyCheckFormService.updateLabSafetyCheckForm(labSafetyCheckForm);
        return AjaxResult.success(labSafetyCheckForm);
    }

    /**
     * 删除安全检查单
     */
    @PreAuthorize("@ss.hasPermi('labsafety:labsafetyForm:edit')")
    @Log(title = "安全检查单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instanceIds}")
    public AjaxResult remove(@PathVariable Long[] instanceIds)
    {
        return toAjax(labSafetyCheckFormService.deleteLabSafetyCheckFormByIds(instanceIds));
    }
}
